<template>
      <div class="app-container">
        <el-form ref="form" :model="form" :rules="formRules" label-width="120px" size="medium">
            <el-form-item label="工作中心" prop="workCenter" >
                <el-select
                clearable
                v-model="form.workCenter"
                style="width:300px"
                >
                    <el-option
                    :value="item"
                    :label="item"
                    v-for="(item,index) in this.centerList"
                    :key="index"/>
                </el-select>
            </el-form-item>
            <el-form-item label="问题大类" prop="issueLarge" >
                <el-input v-model="form.issueLarge" style="width:300px" />
            </el-form-item>
            <el-form-item label="问题小类" prop="issueLittle">
                <el-input v-model="form.issueLittle" style="width:300px" />
            </el-form-item>
            <el-form-item label="缺陷代码" prop="issueId">
                <el-input v-model="form.issueId" style="width:300px" />
            </el-form-item>
            <el-form-item label="问题索引" prop="issueIndexes">
                <el-input v-model="form.issueIndexes" style="width:300px" />
            </el-form-item>
            <el-form-item label="责任环节" prop="responsibility">
                <el-input v-model="form.responsibility" style="width:300px" />
            </el-form-item>
            <el-form-item label="问题性质" prop="natureName" >
                <el-select v-model="form.natureName" @focus="fficientFocus" @change="fficientChange" style="width:300px">
                    <el-option :value="item.natureId" :label="item.natureName" v-for="(item,index) in this.fficientList" :key="index"/>
                </el-select>
            </el-form-item>
            <el-form-item label="考核系数" prop="natureCoefficient">
                <el-input v-model="form.natureCoefficient" disabled style="width:300px" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('form')">添加</el-button>
                <el-button type="primary" @click="resetForm('form')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import { saveIssue,findIssueNature } from '../../api/problem'

export default {
    name: 'add_problem',
    data() {
        return {
            form: {
                workCenter: '',            //工作中心
                issueLarge: '',          //问题大类
                issueLittle: '',        //问题小类
                issueId: '',         //缺陷代码
                issueIndexes: '',           //问题索引
                responsibility: '',          //责任环节
                natureName:'',
                natureCoefficient:'',
            },
            formRules: {
                workCenter: [
                    { required: true, message: '请选择工作中心', trigger: 'blur' }
                ],
                issueLarge: [
                    { required: true, message: '请输入问题大类', trigger: 'blur' }
                ],
                issueLittle: [
                    { required: true, message: '请输入问题小类', trigger: 'blur' }
                ],
                issueId: [
                    { required: true, message: '请输入缺陷代码', trigger: 'blur' }
                ],
                issueIndexes: [
                    { required: true, message: '请输入问题索引', trigger: 'blur' }
                ],
                responsibility: [
                    { required: true, message: '请输入责任环节', trigger: 'blur' }
                ],
                natureName: [
                    { required: true, message: '请输入问题性质', trigger: 'blur' }
                ],
                natureCoefficient: [
                    { required: true, message: '请输入考核系数', trigger: 'blur' }
                ]
            },
            centerList: ['数控车间','装配车间','EMS总装车间','焊接车间','喷涂车间'],
            fficientList: ['轻微问题','一般问题','严重问题','一般批量','严重批量','停线问题'],
        }
    },
    methods: {
        fficientChange(e){
        this.fficientList.forEach(item =>{
          if(e == item.natureId) {
            this.form.natureCoefficient = item.natureCoefficient
          }
        })
      },
        // 问题性质
        async fficientFocus(){
          const res = await findIssueNature({page: 1, limit: 10000})
          this.fficientList = res.data.list
        },
        // 对表单进行重置
        resetForm(formItem) {
            this.$refs[formItem].resetFields()
        },
        // 提交表单之前先进行验证
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.submit()
                } else {
                    return false
                }
            })
        },
        // 提交机台表单信息
        async submit() {
          let query = Object.assign({},this.form)
          query.natureId = this.form.natureName
          delete query.natureName
          delete query.natureCoefficient
          const data = await saveIssue(query)
          const errMsg = data.msg
          if(data.code == '1') {
            this.$message({
              showClose: true,
              message: errMsg,
              type: 'success'
            })
          } else {  
            this.$message({
              showClose: true,
              message: errMsg,
              type: 'warning'
            })
          }
        }
    }
}
</script>

<style>

</style>